<?php
namespace app\controllers;

use app\common\HttpCurl;
use app\common\PayUtil;
use app\logic\CartLogic;
use app\logic\UserLogic;
use Yii;

class CartController extends BaseController
{

    /**
     * 获取默认地址
     * @apiDescription   by Jim
     * @api              {post} /cart/get-default-address 获取默认地址
     * @apiName          get-default-address
     * @apiVersion       1.0.0
     * @apiParam {string} token 用户token
     * @apiGroup         Cart
     * @apiSampleRequest /user/get-default-address?flagsign=1
     * @apiSuccessExample {json} 获取成功
     * {
        "name": "李大唐", //收货人姓名
        "phone": "123432423423",  // 电话
        "province": "省",
        "city": "市",
        "district": "区",
        "address": "地址", // 收货地址
        "addr_id": "", // 地址ID
     }
     * @apiError         40004    获取失败
     */
    public function actionGetDefaultAddress(){
        $userLogic = new UserLogic();
        $address = $userLogic->getDefaultAddress($this->user_id);
        if($address)
            $this->response($address);
        else
            $this->response();
    }

    /**
     * 获取购物车内容
     * @apiDescription   by Jim
     * @api              {post} /cart/get-cart 获取购物车内容
     * @apiName          get-cart
     * @apiVersion       1.0.0
     * @apiParam {string} token 用户token
     * @apiGroup         Cart
     * @apiSampleRequest /user/get-cart?flagsign=1
     * @apiSuccessExample {json} 获取成功
     *
     {
     "skus": [{
    "goods_id":123,
    "sku_id":123,
    "sku_pic":"",
    "goods_name":"***",
    "sku_spec":["黑色","大"],
    "sku_price":90.00,
    "num":1,
    "status":1//该sku是否存在，不存在置灰，不可购买

    }],
    "total_num":1,
    "total_amount":90.00
     }
     * @apiError         50000    获取失败
     */
    public function actionGetCart(){
        $cartLogic = new CartLogic();
        $result = $cartLogic->getCart($this->user_id);
        /*$yx_skuids = array_column($result['skus'],'yx_sku_id');
        $yx_skuids = json_encode($yx_skuids);
        $sku_stock = HttpCurl::post("yanxuan.inventory.count.get",['skuIds'=>$yx_skuids]);
        $sku_stock = json_decode($sku_stock,true);*/


        foreach($result['skus'] AS $key=>&$value){
            $value['sku_pic'] = Yii::$app->params['pic_domain'].$value['sku_pic'];
            $value['sku_spec'] = json_decode($value['sku_spec'],true);
            //$value['status'] = $result['sku_status'][$value['sku_id']];
            /*foreach ($sku_stock as $v){
                if($v['skuId']==$value['yx_sku_id']){
                    if($v['inventory']=0 || $value['num']>$v['inventory'])
                        $value['status'] = 0;
                    else
                        $value['status'] = 1;
                }
            }*/
            /*if($value['inventory']=0 || $value['num']>$value['inventory'] || $value['inventory_status']=0)
                $value['status'] = 0;
            else
                $value['status'] = 1;*/
        }
        $this->response($result);
    }
    /**
     * 加入购物车
     * @apiDescription   by Jim
     * @api              {post} /cart/add-cart 加入购物车
     * @apiName          add-cart
     * @apiVersion       1.0.0
     * @apiParam {string} token 用户token
     * @apiParam {int} sku_id skuid
     * @apiParam {int} num 数量
     * @apiGroup         Cart
     * @apiSampleRequest /user/add-cart?flagsign=1
     * @apiSuccessExample {json} 获取成功
     *
    {

    }
     * @apiError         50001    获取失败
     */
    public function actionAddCart(){
        $sku_id = $this->getQueryparams('sku_id',0);
        $num = $this->getQueryparams('num',0);
        $cartLogic = new CartLogic();
        $result = $cartLogic->addCart($this->user_id,$sku_id,$num);
        $this->response();
    }
    /**
     * 编辑购物车
     * @apiDescription   by Jim
     * @api              {post} /cart/edit-cart 编辑购物车
     * @apiName          edit-cart
     * @apiVersion       1.0.0
     * @apiParam {string} token 用户token
     * @apiParam {int} sku_id skuid
     * @apiParam {int} num 数量
     * @apiGroup         Cart
     * @apiSampleRequest /user/edit-cart?flagsign=1
     * @apiSuccessExample {json} 获取成功
     *
    {

    }
     * @apiError         50002    获取失败
     */
    public function actionEditCart(){
        $sku_id = $this->getQueryparams('sku_id',0);
        $num = $this->getQueryparams('num',0);
        $cartLogic = new CartLogic();
        $result = $cartLogic->editCart($this->user_id,$sku_id,$num);
        $this->response();
    }
    /**
     * 删除购物车商品
     * @apiDescription   by Jim
     * @api              {post} /cart/del-cart 删除购物车商品
     * @apiName          del-cart
     * @apiVersion       1.0.0
     * @apiParam {string} token 用户token
     * @apiParam {int} sku_id skuid
     * @apiGroup         Cart
     * @apiSampleRequest /user/del-cart?flagsign=1
     * @apiSuccessExample {json} 获取成功
     *
    {

    }
     * @apiError         50003    获取失败
     */
    public function actionDelCart(){
        $sku_id = $this->getQueryparams('sku_id',0);
        $cartLogic = new CartLogic();
        $sku_ids = json_decode($sku_id,true);
        foreach ($sku_ids AS $sku_id_one){
            $result = $cartLogic->delCart($this->user_id,$sku_id_one);
        }
        $this->response();
    }

    /**
     * 提交订单
     * @apiDescription   by Jim
     * @api              {post} /cart/submit-order 提交订单
     * @apiName          submit-order
     * @apiVersion       1.0.0
     * @apiParam {string} token 用户token
     * @apiParam {string} name 收件人
     * @apiParam {string} phone 收件人电话
     * @apiParam {string} province 省
     * @apiParam {string} city 市
     * @apiParam {string} district 区
     * @apiParam {string} address 详细地址
     * @apiParam {string} skus json格式,
                                        [{
                                        "sku_id":123,
                                        "sku_pic":"",
                                        "goods_name":"***",
                                        "sku_spec":["黑色","大"],
                                        "sku_price":90.00,
                                        "num":1
                                        }]
     * @apiGroup         Cart
     * @apiSampleRequest /user/submit-order?flagsign=1
     * @apiSuccessExample {json} 获取成功
      {
     "order_no":""
      }
     * @apiError         50004    获取失败
     */

    public function actionSubmitOrder(){
        $name = $this->getQueryparams('name','');
        $phone = $this->getQueryparams('phone','');
        $province = $this->getQueryparams('province','');
        $city = $this->getQueryparams('city','');
        $district = $this->getQueryparams('district','');
        $address = $this->getQueryparams('address','');
        $skus = $this->getQueryparams('skus','');
        $channel_code = $this->getQueryparams('channel_code','');
        $is_buynow = $this->getQueryparams('is_buynow',1);
        $skus = json_decode($skus,true);
        $sku_ids = array_column($skus,'sku_id');
        $cartLogic = new CartLogic();
        $result1 = $cartLogic->getGoodsSkuAmount($sku_ids,$skus,$goods_amount,$channel_amount,$goods_name,$allon,$stock_status,$no_goods_name);
        if(!$allon){
            return $this->error('50004',"订单中的".$no_goods_name."已经下架，请删除重新提交");
        }
        if(!$stock_status){
            return $this->error('50004',"手慢了，选择的".$no_goods_name."产品已被抢光，请重新下单。");
        }
        if(!$result1){
            return $this->error('50004',"订单中有商品规格价格已经发生变化，请删除重新提交");
        }
        /*$yx_sku_ids = array_column($skus,'yx_sku_id');
        $yx_sku_ids = json_encode($yx_sku_ids);
        $sku_stock = HttpCurl::post("yanxuan.inventory.count.get",['skuIds'=>$yx_sku_ids]);
        $sku_stock = json_decode($sku_stock,true);
        foreach ($sku_stock AS $v){
            if($v['inventory']=0)
                return $this->error(['50004',"订单中有商品已经没有库存，请重新提交"]);
        }*/
        $result = $cartLogic->submitOrder($this->user_id,$name,$phone,$province,$city,$district,$address,$skus,$goods_amount,$channel_amount,$channel_code,$order_no,$order_time,$order_amount,$is_buynow);
        if($result){
            $pu = new PayUtil();

            $url = $pu->getUrl($order_no,date('YmdHis',strtotime($order_time)),$order_amount,$goods_name);
            //echo $url;exit;
            $this->response(['pay_url'=>$url]);
        }

    }





}
